import { getAllSurveyUnitList, getDistrictCascader } from '/@/api/demo/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { checkID } from '/@/utils/index';
import debounce from 'lodash.debounce';
import { TreeSelect } from 'ant-design-vue';
const { SHOW_PARENT, SHOW_ALL, SHOW_CHILD } = TreeSelect;
import { useUserStore } from '/@getAllSurveyUnitList/store/modules/user';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { encryptByMd5, strongPwdRegex } from '/@/utils/cipher';

export const columns: BasicColumn[] = [
  {
    title: '任务包名称',
    dataIndex: 'name',
  },
  {
    title: '任务包指派',
    dataIndex: 'surveyUnitName',
  },
  {
    title: '抽样比例',
    dataIndex: 'sampleProportion',
  },
  {
    title: '抽样条数',
    dataIndex: 'sampleCount',
  },
  {
    title: '初判结果',
    dataIndex: 'sampleType',
    customRender: ({ record }) => {
      const status = record.sampleType;
      let color = 'green';
      let text = '-/-';
      if (0 == status) {
        color = 'green';
        text = '无隐患';
      } else if (1 == status) {
        color = 'yellow';
        text = '存在一般隐患';
      } else if (2 == status) {
        color = 'red';
        text = '严重安全隐患';
      }
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '街道',
    dataIndex: 'sampleAreaCodes',
    customRender: ({ record }) => {
      const sampleAreaCodes = record.sampleAreaCodes;
      return h(Tag, {}, () => text);
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createOn',
    width: 200,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '任务包名称',
    component: 'Input',
    labelWidth: '6em',
    colProps: { span: 8 },
  },
  {
    field: 'surveyUnitName',
    label: '任务包指派',
    component: 'Input',
    labelWidth: '6em',
    colProps: { span: 8 },
  },
];

export const accountPwdFormSchema: FormSchema[] = [
  {
    field: 'pwd',
    label: '密码',
    defaultValue: '',
    component: 'InputPassword',
    rules: [
      {
        required: true,
        message: '请输入新密码',
      },
      {
        pattern: strongPwdRegex,
        // message: '密码必须为八位及以上并且大小写字母数字特殊字符三项都要包括',
        message: '密码中必须包含字母、数字、特殊字符，(长度在 8-30位)！',
      },
    ],
  },
];

export function getEditFormSchema(parId) {
  const accountFormSchema: FormSchema[] = [
    {
      field: 'name',
      label: '任务包名称',
      component: 'Input',
      required: true,
    },
    {
      label: '任务包指派',
      field: 'surveyUnitId',
      component: 'ApiSelect',
      componentProps: {
        api: getAllSurveyUnitList,
        labelField: 'name',
        valueField: 'id',
      },
      required: true,
    },
    {
      field: 'sampleProportion',
      label: '抽样比例',
      component: 'InputNumber',
      componentProps: {
        addonAfter: '%',
        min: 0,
        max: 100,
      },
      required: true,
    },
    {
      label: '初判结果',
      field: 'sampleType',
      component: 'Select',
      componentProps: {
        options: [
          { label: '无隐患', value: 0 },
          { label: '存在一般隐患', value: 1 },
          { label: '严重安全隐患', value: 2 },
        ],
        placeholder: '请选择初判结果',
      },
    },
    {
      field: 'sampleAreaCodes_',
      component: 'ApiCascader',
      helpMessage: ['双击也可以选择'],
      label: '街道',
      componentProps: ({ formModel, formActionType }) => {
        return {
          api: getDistrictCascader,
          apiParamKey: 'parentId',
          labelField: 'regionName',
          valueField: '_d',
          dataField: 'regionCode',
          asyncFetchParamKey: 'parentId',
          multiple: true,
          initFetchParams: {
            parentId: parId,
          },
          isLeaf: (record) => {
            return record.regionalLevel > 6;
          },
          onChange: (keys, args, apiData, options) => {},
        };
      },
    },
  ];

  return accountFormSchema;
}
